package com.ruoyi.ai.tools;

import com.ruoyi.ai.domain.YaDailyTasks;
import com.ruoyi.ai.domain.YaInterviewMasteryDetail;
import com.ruoyi.ai.domain.YaStudentScores;
import com.ruoyi.ai.mapper.YaDailyTasksMapper;
import com.ruoyi.ai.mapper.YaInterviewMasteryDetailMapper;
import com.ruoyi.ai.mapper.YaWeeklyReportsMapper;
import com.ruoyi.ai.service.VectorService;
import com.ruoyi.ai.domain.YaTeachingPlan;
import com.ruoyi.ai.mapper.YaTeachingPlanMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author coach tam
 * @email 327395128@qq.com
 * @values 坚持灵活 灵活坚持
 * @since 2025/3/22
 */
@Slf4j
@Component
public class ChatTools {

    String getCurrentDateTime() {
        log.debug("获取系统时间");
        return LocalDateTime.now().atZone(LocaleContextHolder.getTimeZone().toZoneId()).toString();
    }

    String queryAssistantName(){
        log.debug("tool:查询助手信息");
        return """
                雅橙科技聊天助手
                """;
    }

    @Autowired
    private YaDailyTasksMapper yaDailyTasksMapper;
    @Autowired
    private YaInterviewMasteryDetailMapper yaInterviewMasteryDetailMapper;
    @Autowired
    private YaWeeklyReportsMapper yaWeeklyReportsMapper;

    String queryUserName(){
        log.debug("tool:查询客户端用户信息");

        List<YaInterviewMasteryDetail> yaInterviewMasteryDetails = yaInterviewMasteryDetailMapper.selectYaInterviewMasteryDetailList();
        List<YaDailyTasks> dailyTasks = yaDailyTasksMapper.selectYaDailyTasksList();
        List<YaStudentScores> studentScores = yaWeeklyReportsMapper.selectYaWeeklyReportsList();

        StringBuilder result = new StringBuilder();
        // 处理面试题掌握详情列表
        for (YaInterviewMasteryDetail detail : yaInterviewMasteryDetails) {
            result.append(" 用户ID：").append(detail.getUserId()).append("，目前面试题掌握情况(0-10分)：");
            result.append(detail.getQuestion()).append(" ")
                  .append(detail.getScore()).append("分；");
            result.append("\n");
        }
        // 处理每日任务列表
        for (YaDailyTasks task : dailyTasks) {
            result.append(" 用户ID：").append(task.getStudentId()).append("，每日任务完成情况：日期 ")
                  .append(task.getDate() != null ? task.getDate() : "");
            result.append("；课程内容 ").append(task.getCourseContent());
            result.append("；状态 ").append(task.getStatus());
            result.append("\n");
        }
        // 处理学生周考成绩列表
        for (YaStudentScores score : studentScores) {
            result.append(" 用户ID：").append(score.getStudentId()).append("，每周测试成绩：第几周 ")
                  .append(score.getWeek());
            if (score.getWrittenScore() != null || score.getPracticalScore() != null || score.getInterviewScore() != null) {
                if (score.getWrittenScore() != null) {
                    result.append("；笔试成绩 ").append(score.getWrittenScore());
                }
                if (score.getPracticalScore() != null) {
                    result.append("；机试成绩 ").append(score.getPracticalScore());
                }
                if (score.getInterviewScore() != null) {
                    result.append("；面试成绩 ").append(score.getInterviewScore());
                }
            } else {
                result.append("；缺考");
            }
            result.append("\n");
        }

        return result.toString();
    }

    @Autowired
    private VectorService vectorService;

    String route(String userInput) {
        log.debug("tool:根据用户输入查询前端路由");

        try {
            // 从向量数据库中查询最匹配的页面
            List<String> results = vectorService.searchQuestion2(userInput);
            if (results != null && !results.isEmpty()) {
                // 解析返回结果，格式为 "页面名称:路由地址"
                return results.toString(); // 返回路由地址
            }
        } catch (Exception e) {
            log.error("路由查询失败", e);
        }

        // 默认返回空字符串，表示未找到匹配路由
        return "";
    }

    @Autowired
    private YaTeachingPlanMapper yaTeachingPlanMapper;

    String queryTeachingPlan() {
        log.debug("tool:查询课表");
        try {
            // 查询课表信息
            List<YaTeachingPlan> teachingPlanList = yaTeachingPlanMapper.selectTeachingPlan();
            if (teachingPlanList == null || teachingPlanList.isEmpty()) {
                return "当前没有课表信息";
            }
        
            return teachingPlanList.toString();
        } catch (Exception e) {
            log.error("查询课表时发生异常", e);
            return "系统异常，无法查询课表";
        }
    }

    String queryCourse() {
        log.debug("tool:查询课程信息");
        return """
                🌟《AI+Java全栈精英计划》——抢占未来就业风口，成就高薪复合人才！🌟
               【课程亮点抢先看】
               🔥 双核引擎：AI技术+Java开发深度融合，突破传统CRUD瓶颈，打造企业级智能系统开发能力
               💸 薪资跳板：掌握AI的Java工程师平均薪资溢价30%-40%（2024权威数据）
               🚀 企业刚需：深度对接Spring Cloud分布式架构 x LangChain/RAG等前沿AI框架
                """;
    }
}
